package com.yuma.springaitool.config;

import com.alibaba.cloud.ai.autoconfigure.dashscope.DashScopeChatProperties;
import com.alibaba.cloud.ai.dashscope.chat.DashScopeChatModel;
import com.alibaba.cloud.ai.dashscope.chat.DashScopeChatOptions;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.tool.ToolCallbackProvider;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AiConfig {

    @Bean
    public ChatClient workChatClient(DashScopeChatModel chatModel,
                                     DashScopeChatProperties options,
                                     ToolCallbackProvider toolCallbackProvider,
                                     ChatMemory chatMemory) {
        DashScopeChatOptions dashScopeChatOptions = DashScopeChatOptions.fromOptions(options.getOptions());
        dashScopeChatOptions.setTemperature(0.7);

        return  ChatClient.builder(chatModel)
                .defaultSystem("""
                        # 业务员任务拆分规则
                        ## 1.要求
                        ### 1.1 根据用户内容识别任务
                        
                        ## 2. 任务
                        ### 2.1 JobType:技术(TECHNOLOGY) 要求学员提供姓名和班级号和问题， 或者从对话中提取；
                        ### 2.2 JobType:报名(CONSULT) 要求用户提供报名学习的技术方向， 或者从对话中提取；
                        ### 2.3 JobType:其他(OTHER)
                        """)
                .defaultAdvisors(
                        MessageChatMemoryAdvisor.builder(chatMemory).build()
                )
                .defaultOptions(dashScopeChatOptions)
                .defaultToolCallbacks(toolCallbackProvider)
                .build();
    }

    @Bean
    public ChatClient botChatClient(DashScopeChatModel chatModel,
                                    DashScopeChatProperties options,
                                    ToolCallbackProvider toolCallbackProvider,
                                    ChatMemory chatMemory) {

        DashScopeChatOptions dashScopeChatOptions = DashScopeChatOptions.fromOptions(options.getOptions());
        dashScopeChatOptions.setTemperature(0.3);
        return  ChatClient.builder(chatModel)
                .defaultSystem("""
                           你是御码科技公司的一名业务员，你可以回答关于御码科技有限公司的信息。
                           包括每个老师所教的专业方向和学生成绩。
                            """)
                .defaultAdvisors(
                        MessageChatMemoryAdvisor.builder(chatMemory).build()
                )
                .defaultOptions(dashScopeChatOptions)
                .defaultToolCallbacks(toolCallbackProvider)
                .build();
    }
}